package com.ycsj.goldmedalnewconcept.parent.activity;

import android.content.Context;
import android.content.Intent;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.makeramen.roundedimageview.RoundedImageView;
import com.ycsj.common.base.BaseActivity;
import com.ycsj.common.base.BaseApplication;
import com.ycsj.common.base.BusInfo;
import com.ycsj.common.image.ImageUtil;
import com.ycsj.common.manager.CustomDialogManager;
import com.ycsj.common.utils.GsonUtil;
import com.ycsj.common.utils.MaxTextLengthFilter;
import com.ycsj.common.view.StarClickBar;
import com.ycsj.goldmedalnewconcept.R;
import com.ycsj.goldmedalnewconcept.common.HttpAddress;
import com.ycsj.goldmedalnewconcept.common.bean.ResultInfo;
import com.ycsj.goldmedalnewconcept.common.http.HttpUtils;
import com.ycsj.goldmedalnewconcept.common.util.AccountUtils;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;

/**
 * 添加新的评价
 * Created by Administrator on 2018/6/21 0021.
 */

public class ParentsAddNewEvaluateActiivty extends BaseActivity implements View.OnClickListener {

    //评分控件
    private StarClickBar starBar;
    private TextView tvTitle;
    private ImageView ivBack;
    private RoundedImageView ivHead;
    /**
     * 张三
     */
    private TextView tvName;
    /**
     * 性别:男
     */
    private TextView tvGender;
    /**
     * 电话:13000000000
     */
    private TextView tvTel;
    private StarClickBar bar;
    /**
     * 请输入评语
     */
    private EditText etInput;
    /**
     * 0/100
     */
    private TextView tvNum;
    private Button btCommit;

    private String userclass;
    private String teacher;
    private String name;
    private String sex;
    private String headUrl;

    /**
     * @param context
     * @param userclass   学生班级
     * @param teachertel  学生老师的账号
     * @param teacherName 学生老师的名称
     * @param sex         性别
     */
    public static void goActivity(Context context, String userclass, String teachertel, String teacherName, String sex, String headUrl) {
        Intent it = new Intent(context, ParentsAddNewEvaluateActiivty.class);
        it.putExtra("userclass", userclass);
        it.putExtra("teacher", teachertel);
        it.putExtra("teachername", teacherName);
        it.putExtra("sex", sex);
        it.putExtra("headurl", headUrl);
        context.startActivity(it);
    }


    @Override
    protected int getContentLayout() {
        return R.layout.parent_activity_add_new_evaluate;
    }

    @Override
    protected void init() {

        initStateBar();

        //初始化控件
        initView();

        //接收数据
        initIntent();

        //设置监听
        initListener();
    }

    /**
     * 接收数据
     */
    private void initIntent() {
        userclass = getIntent().getStringExtra("userclass");
        teacher = getIntent().getStringExtra("teacher");
        name = getIntent().getStringExtra("teachername");
        sex = getIntent().getStringExtra("sex");
        headUrl = getIntent().getStringExtra("headurl");

        //设置姓名
        tvName.setText(name);
        //设置电话
        tvTel.setText(teacher);
        //设置性别
        if ("1".equals(sex)) {
            tvGender.setText("性别：男");
        } else if ("2".equals(sex)) {
            tvGender.setText("性别：女");
        }
        //设置头像
        ImageUtil.displayImage(this, ivHead, headUrl, R.drawable.head_default);
    }

    /**
     * 初始化控件
     */
    private void initView() {
        starBar = findViewById(R.id.bar);
        starBar.setStarMark(0f);
        starBar.setIntegerMark(true);
        tvTitle = findViewById(R.id.tv_title);
        ivBack = findViewById(R.id.iv_back);
        ivBack.setOnClickListener(this);
        ivHead = findViewById(R.id.iv_head);
        tvName = findViewById(R.id.tv_name);
        tvGender = findViewById(R.id.tv_gender);
        tvTel = findViewById(R.id.tv_tel);
        bar = findViewById(R.id.bar);
        etInput = findViewById(R.id.et_input);
        tvNum = findViewById(R.id.tv_num);
        btCommit = findViewById(R.id.bt_commit);
        btCommit.setOnClickListener(this);
        tvTitle.setText("添加新的评价");
    }


    /**
     * 设置监听的方法
     */
    private void initListener() {
        etInput.setFilters(new InputFilter[]{new MaxTextLengthFilter(200)});
        etInput.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                String content = etInput.getText().toString().trim();
                if (!TextUtils.isEmpty(content)) {
                    if (content.length() > 200) {
                        tvNum.setText("200/200");
                    } else {
                        tvNum.setText(content.length() + "/200");
                    }
                } else {
                    tvNum.setText("0/200");
                }
            }
        });
    }


    /**
     * 页面控件点击事件
     *
     * @param v
     */
    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.iv_back) {
            //返回
            finish();

        } else if (id == R.id.bt_commit) {
            //提交
            String content = etInput.getText().toString().trim();

            String score = bar.getStarMark() + "";
            if (TextUtils.isEmpty(content)) {
                ToastUtils.showShort("内容不能为空");
                return;
            }

            if (TextUtils.isEmpty(score)) {
                ToastUtils.showShort("评分不能为空");
                return;
            }

            if (bar.getStarMark() == 0) {
                ToastUtils.showShort("评分不能为空");
                return;
            }

            if ("0".equals(score)) {
                ToastUtils.showShort("评分不能为空");
                return;
            }

            //上传服务器
            httpCommit(content, score);
        }
    }

    /**
     * 提交评论
     */
    private void httpCommit(String content, String score) {

        Map<String, String> map = new HashMap<>();
        map.put("type", "8");
        map.put("c", AccountUtils.getAccount(this));
        map.put("role", AccountUtils.getRole(this));
        map.put("user_class", userclass);
        map.put("teachermobile", teacher);
        map.put("score", ((int) Float.parseFloat(score)) + "");
        map.put("content", content);
        map.put("status", "0");

        LogUtils.iTag("test", "user_class=" + userclass + "&score=" + score
                + "&user_class" + teacher);

        CustomDialogManager.show(this, "提交中...");

        HttpUtils.post(HttpAddress.ADDEVALUATERECORD, map, this, new HttpUtils.CallBack() {
            @Override
            public void onError(Call call, Exception e, int id) {
                CustomDialogManager.dissmiss();
                ToastUtils.showShort(BaseApplication.ERROR);
            }

            @Override
            public void onResponse(String response, int id) {
                CustomDialogManager.dissmiss();
                if (!TextUtils.isEmpty(response)) {
                    ResultInfo info = null;
                    try {
                        info = GsonUtil.jsonToBean(response, ResultInfo.class);
                    } catch (Exception e) {
                        Log.i("test", "数据解析异常");
                    }

                    if (null != info) {
                        if ("1".equals(info.state)) {
                            //返回成功
                            BusInfo busInfo = new BusInfo();
                            busInfo.type = ParentsEvaluateTeacherActivity.EVALUATEUPDATE;
                            EventBus.getDefault().post(busInfo);
                            finish();
                            ToastUtils.showShort("提交成功");

                        } else {
                            ToastUtils.showShort("提交失败,返回错误码:" + info.state);
                            Log.i("test", "返回器返回异常,错误码是:" + info.state);
                        }
                    }
                } else {
                    Log.i("test", "服务返回数据异常");
                }
            }
        });
    }
}
